// mircoApp/horizon/component/raisePricePopup/raisePricePopup.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showType: {
      type: String,
      value: ''
    },
    show: {
      type: Boolean,
      value: false
    },
    childGood: {
      type: null,
    },
    resultArr: {
      type: Array,
      value: []
    },
    infoData: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    batchValue: '',
    ratioValue: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    inp() {
      console.log('dddddddddd')
    },
    emit(e) {
      console.log(e)
      let {
        key
      } = e.currentTarget.dataset;
      if (key == 'batch') {
        if (this.data.resultArr.length <= 0) {
          app.tips('请选择批量加价商品')
          return
        }
      }
      this.triggerEvent("emit", {
        show: true,
        showType: key
      })
    },
    ratioCancel() {
      this.triggerEvent("emit", {
        show: true,
        showType: 'setting'
      })
    },
    inpAdd(e) {
      console.log(e, this.data.showType)
      if (this.data.showType == 'sigleOption') {
        this.data.childGood.option_list[0].add_price = e.detail.value;
        this.setData({
          childGood: this.data.childGood
        })
      } else {
        let ind = e.currentTarget.dataset.ind;
        let val = e.detail.value;
        this.data.childGood.option_list[ind].add_price = val;
        this.setData({
          childGood: this.data.childGood
        })
      }

    },
    confirm(e) {
      let {
        key
      } = e.currentTarget.dataset;
      let obj = {
        show: false,
        showType: key
      }
      if (key == 'moreOption' || key == 'sigleOption') {
        // console.log(this.data.childGood)
        this.postCountMethod(obj)
        return
      } else if (key == 'batch') {
        // 批量加价
        let rarr = this.data.resultArr;
        let selectArr = [];
        rarr.forEach(element => {
          if (this.data.infoData[element].id) {
            selectArr.push(this.data.infoData[element].goods_id)
          }
        });
        this.postCountMethod(obj, selectArr)
      } else if (key == 'ratio') {
        console.log(this.data.childGood)
        this.postCountMethod(obj)
      } else {
        this.triggerEvent("emit", obj);
      }
    },
    closeTap(e) {
      let {
        key
      } = e.currentTarget.dataset;
      let obj = {
        show: false,
        showType: this.data.showType
      }
      if (key == 'ratio' || key == 'batch') {
        obj.show = true;
        obj.showType = 'setting';
      }
      this.triggerEvent("emit", obj);
      this.setData({
        batchValue: '',
        ratioValue: ''
      })
    },
    postCountMethod(obj, selectArr) {
      let _url, json;
      if (this.data.showType == 'sigleOption' || this.data.showType == 'moreOption') {
        _url = "plugin.horizon.frontend.owner.changePrice";
        json = {
          goods_id: this.data.childGood.goods_id,
          owner_data: this.data.childGood
        }
      } else if (this.data.showType == 'ratio') {
        _url = "plugin.horizon.frontend.owner.setDefaultPercent";
        json = {
          percent: this.data.ratioValue,
        }
      } else {
        _url = "plugin.horizon.frontend.owner.batchChangePrice";
        json = {
          goods_ids: selectArr,
          percent: this.data.batchValue
        }
        console.log(json)
      }
      let urlStr = app.getNetAddresss(_url);
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            app.tips(res.msg)
            this.triggerEvent("emit", obj);
          } else {
            app.tips(res.msg)
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
  }
})